package com.ruoyi.zsyh.controller.applet.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ZtreeString;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 附近店铺Controller
 */
@Controller
@RequestMapping("/applet/zsyh/shop")
public class ZsyhShopAppletController extends BaseController
{


    @Autowired
    private IZsyhServiceOrderService zsyhServiceOrderService;

    @Autowired
    private IZsyhUserRepairstoreService zsyhUserRepairstoreService;

    @Autowired
    private IZsyhUserRepairareaService zsyhUserRepairareaService;

    @Autowired
    private IZsyhStoreCategoryService zsyhStoreCategoryService;

    @Autowired
    private WxUserController wxUserController;

    @Autowired
    private IZsyhAgentAreaService zsyhAgentAreaService;

    @Autowired
    private IZsyhPlatformUserService zsyhPlatformUserService;

    @Autowired
    private IZsyhUserMessageService zsyhUserMessageService;

    @Autowired
    private IZsyhServiceTypeService zsyhServiceTypeService;

    @Autowired
    private IZsyhPersonOrderService zsyhPersonOrderService;

    @Autowired
    private IZsyhPartsQrcodeInstallService zsyhPartsQrcodeInstallService;





    /**
     * 获取附近店铺列表列表
     * @param storeClassify 店铺类型（1维修店2配件店3电器店）
     * @return
     */
    @RequestMapping("/getNearbyStoreList")
    @ResponseBody
    public AjaxResult getNearbyStoreList(String storeClassify, String pageNum, String pageSize, String key)throws Exception
    {
        ZsyhUserRepairstore userPartsstore = new ZsyhUserRepairstore();
//        userPartsstore.setStoreClassify(storeClassify);
//        userPartsstore.setLat(lat);
//        userPartsstore.setLon(lon);
        userPartsstore.setStoreState("2");

        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        map.put("key", key);
        userPartsstore.setParams(map);
        List<ZsyhUserRepairstore> list = zsyhUserRepairstoreService.selectZsyhUserRepairstoreListApplet(userPartsstore);
        System.out.println("list.size() ---------------------" + list.size());
        return success("成功", list);
    }

    /**
     * 配件店详情
     * @return
     */
    @RequestMapping("/getShopDetails")
    @ResponseBody
    public AjaxResult getShopDetails(String id)throws Exception
    {

        ZsyhUserRepairstore userPartsstore = new ZsyhUserRepairstore();
//        userPartsstore.setLat(lat);
//        userPartsstore.setLon(lon);
        userPartsstore.setId(id);
        ZsyhUserRepairstore repairstore = zsyhUserRepairstoreService.selectZsyhUserStoreDistanceById(userPartsstore);

        ZsyhServiceOrder order = new ZsyhServiceOrder();
        order.setOrderState("4");
        order.setRepairStoreId(id);
        Map map = new HashedMap();
        map.put("beginPos", 0);
        map.put("pageSize", 2);
        order.setParams(map);
        List<ZsyhServiceOrder> orderList = zsyhServiceOrderService.selectZsyhServiceOrderList(order);
        repairstore.setZsyhServiceOrderList(orderList);
        ZsyhUserRepairstore userPartsstore1 = new ZsyhUserRepairstore();
        userPartsstore1.setId(id);
        userPartsstore1.setStorePageviews(repairstore.getStorePageviews() + 1);
        zsyhUserRepairstoreService.updateZsyhUserRepairstoreApplet(userPartsstore1);
        return success("成功", repairstore);
    }

    /**
     * 获取店铺信息
     * @return
     */
    @RequestMapping("/getStoreByUserId")
    @ResponseBody
    @Transactional
    public AjaxResult getStoreByUserId(String userId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        return AjaxResult.success("获取成功", store);
    }



    /**
     * 用户入住为师傅
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/shopApply")
    @ResponseBody
    @Transactional
    public AjaxResult shopApply(@RequestBody ZsyhUserRepairstore userStore)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userStore.getUserId());
        userStore.setId(IdWorker.getId());
        if(store != null ){
            return error("您已经是维修师傅，请勿重复申请");
        }

        store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByPhone(userStore.getStorePhone());
        //已经是师傅或者店铺
        if(store != null ){
            return error("您已经是店铺，请勿重复申请");
        }
        //修改用户主表，userType 用户类型（1普通客户2维修师傅 ）
        ZsyhPlatformUser user = new ZsyhPlatformUser();
        user.setId(userStore.getUserId());
        user.setUserType("2");
        zsyhPlatformUserService.updateZsyhPlatformUser(user);

        //维修店入驻成功，添加公共消息
        ZsyhUserMessage message = new ZsyhUserMessage();
        message.setMessageContent(userStore.getStorePerson() + "已成功入驻平台！");
        message.setMessageStatus("0");
        message.setMessageType("1");
        zsyhUserMessageService.insertZsyhUserMessage(message);

        //如果是市县管理商 则添加isRaker
        user = zsyhPlatformUserService.selectZsyhPlatformUserById(userStore.getUserId());
        if("1".equals(user.getIsCity())){
            userStore.setIsRaker("1");
        }
        userStore.setStoreClassify("1");

        String qrCodeImg = wxUserController.postMiniqrQr(userStore.getId(), userStore.getStoreClassify());
        userStore.setQrcodeImg(qrCodeImg);

        Date date = new Date();
        String areaId = "";
        if(EmptyUtils.isNotEmpty(userStore.getAreasJson())){//服务区域不为空
            //解析服务区域json数据
            List<Map<String, Object>> list = JSON.parseObject(userStore.getAreasJson(), new TypeReference<List<Map<String, Object>>>() {});
            List<ZsyhUserRepairarea> areaList = new ArrayList<>();
            for(Map<String, Object> a:list){
                String pCode = String.valueOf(a.get("pCode"));
                String pName = String.valueOf(a.get("pName"));
                String cCode = String.valueOf(a.get("cCode"));
                String cName = String.valueOf(a.get("cName"));
                String aCode = String.valueOf(a.get("aCode"));
                String aName = String.valueOf(a.get("aName"));
                areaId = aCode;
                ZsyhUserRepairarea area = ZsyhUserRepairarea.getInstance();
                area.setId(IdWorker.getId());
                area.setRepairId(userStore.getId());
                area.setRepairProvince(pCode);
                area.setRepairProvinceName(pName);
                area.setRepairCity(cCode);
                area.setRepairCityName(cName);
                area.setRepairDistrict(aCode);
                area.setRepairDistrictName(aName);
                area.setCreateTime(date);
                area.setType("1");
                areaList.add(area);

//                userStore.setStoreProvince(pCode);
//                userStore.setStoreProvinceName(pName);
//                userStore.setStoreCity(cCode);
//                userStore.setStoreCityName(cName);
//                userStore.setStoreDistrict(aCode);
//                userStore.setStoreDistrictName(aName);

            }
            if(areaList.size() > 0){
                zsyhUserRepairareaService.deleteZsyhUserRepairareaByRepairId(userStore.getId());//先删除原有的，然后再保存新的
                zsyhUserRepairareaService.insertZsyhUserRepairareaBatch(areaList);
            }
        }
        //保存大区会长id
        ZsyhAgentArea zsyhAgentArea = new ZsyhAgentArea();
        zsyhAgentArea.setAgentDistrict(areaId);
        List<ZsyhAgentArea> agentAreaList = zsyhAgentAreaService.selectZsyhAgentAreaList(zsyhAgentArea);
        if(agentAreaList.size() > 0){
            userStore.setAgentId(agentAreaList.get(0).getAgentId());
        }

        if(EmptyUtils.isNotEmpty(userStore.getCategoryJson())){//服务类目不为空
            //解析服务类目json数据
            List<Map<String, Object>> list = JSON.parseObject(userStore.getCategoryJson(), new TypeReference<List<Map<String, Object>>>() {});
            List<ZsyhStoreCategory> catList = new ArrayList<>();
            for(Map<String, Object> a:list){
                String categoryName = String.valueOf(a.get("categoryName"));
                String categoryPrice = String.valueOf(a.get("categoryPrice"));

                ZsyhStoreCategory category = ZsyhStoreCategory.getInstance();
                category.setId(IdWorker.getId());
                category.setStoreId(userStore.getId());
                category.setCategoryName(categoryName);
                category.setCategoryPrice(categoryPrice);
                category.setCreateTime(date);
                catList.add(category);
            }
            if(catList.size() > 0){
                zsyhStoreCategoryService.deleteZsyhStoreCategoryByStoreId(userStore.getId());//先删除原有的，然后再保存新的
                zsyhStoreCategoryService.insertZsyhStoreCategoryBatch(catList);

            }
        }
        zsyhUserRepairstoreService.insertZsyhUserRepairstoreApplet(userStore);
        return success("成功");
    }

    /**
     * 维修师傅申请成为鉴定点
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/shopApplyAppraisal")
    @ResponseBody
    @Transactional
    public AjaxResult shopApplyAppraisal(String userId, String storeName, String storeImage, String storeLicense, String storeAddress)throws Exception
    {

        ZsyhUserRepairstore _store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        ZsyhUserRepairstore store = new ZsyhUserRepairstore();
        store.setId(_store.getId());
        store.setStoreName(storeName);
        store.setStoreImage(storeImage);
        store.setStoreLicense(storeLicense);
        store.setStoreAddress(storeAddress);
        store.setAppraisalState("1");
        store.setIsAppraisal("1");
        zsyhUserRepairstoreService.updateZsyhUserRepairstoreApplet(store);

        return success("成功");
    }

    /**
     * 维修师傅申请成为网点
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/shopApplyRaker")
    @ResponseBody
    @Transactional
    public AjaxResult shopApplyRaker(String userId, String storeName, String storeImage, String storeLicense, String storeAddress, String areaListJson)throws Exception
    {

        ZsyhUserRepairstore _store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        ZsyhUserRepairstore store = new ZsyhUserRepairstore();
        store.setId(_store.getId());
        store.setStoreName(storeName);
        store.setStoreImage(storeImage);
        store.setStoreLicense(storeLicense);
        store.setStoreAddress(storeAddress);
        store.setStoreState("1");
        store.setIsRaker("1");


        List<Map<String, Object>> list = JSON.parseObject(areaListJson, new TypeReference<List<Map<String, Object>>>() {});
        List<ZsyhUserRepairarea> areaList = new ArrayList<>();
        Date date = new Date();
        String areaId = "";
        for(Map<String, Object> a:list) {
            String pCode = String.valueOf(a.get("pCode"));
            String pName = String.valueOf(a.get("pName"));
            String cCode = String.valueOf(a.get("cCode"));
            String cName = String.valueOf(a.get("cName"));
            String aCode = String.valueOf(a.get("aCode"));
            String aName = String.valueOf(a.get("aName"));
            areaId = aCode;
            ZsyhUserRepairarea area = ZsyhUserRepairarea.getInstance();
            area.setId(IdWorker.getId());
            area.setRepairId(_store.getId());
            area.setRepairProvince(pCode);
            area.setRepairProvinceName(pName);
            area.setRepairCity(cCode);
            area.setRepairCityName(cName);
            area.setRepairDistrict(aCode);
            area.setRepairDistrictName(aName);
            area.setCreateTime(date);
            area.setType("2");
            areaList.add(area);
        }
        //保存大区会长id
        ZsyhAgentArea zsyhAgentArea = new ZsyhAgentArea();
        zsyhAgentArea.setAgentDistrict(areaId);
        List<ZsyhAgentArea> agentAreaList = zsyhAgentAreaService.selectZsyhAgentAreaList(zsyhAgentArea);
        if(agentAreaList.size() > 0){
            store.setAgentId(agentAreaList.get(0).getAgentId());
        }
        zsyhUserRepairstoreService.updateZsyhUserRepairstoreApplet(store);

        if(areaList.size() > 0){
            zsyhUserRepairareaService.deleteZsyhUserRepairareaByRepairId(_store.getId());//先删除原有的，然后再保存新的
            zsyhUserRepairareaService.insertZsyhUserRepairareaBatch(areaList);
        }

        return success("成功");
    }

    /**
     * 判断这个区域有没有存在网点
     * @param areaId
     * @return
     * @throws Exception
     */
    @RequestMapping("/judgeAreaExist")
    @ResponseBody
    @Transactional
    public AjaxResult judgeAreaExist(String areaId)throws Exception
    {
        ZsyhUserRepairarea area = new ZsyhUserRepairarea();
        area.setRepairDistrict(areaId);
        area.setType("2");
        List<ZsyhUserRepairarea> areaList = zsyhUserRepairareaService.selectZsyhUserRepairareaList(area);
        if(areaList.size() > 0){
            return error("该区域已有合作网点，请您重新选择！");
        }

        return success("没有合作网点，可以选择");
    }


    /**
     * 修改店铺信息
     * @return
     */
    @RequestMapping("/updateShopInformation")
    @ResponseBody
    @Transactional
    public AjaxResult updateShopInformation(@RequestBody ZsyhUserRepairstore userStore)throws Exception
    {

        String id = userStore.getId();
        ZsyhUserRepairstore _userStore = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(id);
        String storeState = _userStore.getStoreState();

        Date date = new Date();
        String areaId = "";
        if(EmptyUtils.isNotEmpty(userStore.getAreasJson())){//服务区域不为空
            //解析服务区域json数据
            List<Map<String, Object>> list = JSON.parseObject(userStore.getAreasJson(), new TypeReference<List<Map<String, Object>>>() {});
            List<ZsyhUserRepairarea> areaList = new ArrayList<>();
            for(Map<String, Object> a:list){
                String pCode = String.valueOf(a.get("pCode"));
                String pName = String.valueOf(a.get("pName"));
                String cCode = String.valueOf(a.get("cCode"));
                String cName = String.valueOf(a.get("cName"));
                String aCode = String.valueOf(a.get("aCode"));
                String aName = String.valueOf(a.get("aName"));
                areaId = aCode;
                ZsyhUserRepairarea area = ZsyhUserRepairarea.getInstance();
                area.setId(IdWorker.getId());
                area.setRepairId(userStore.getId());
                area.setRepairProvince(pCode);
                area.setRepairProvinceName(pName);
                area.setRepairCity(cCode);
                area.setRepairCityName(cName);
                area.setRepairDistrict(aCode);
                area.setRepairDistrictName(aName);
                area.setCreateTime(date);
                if("0".equals(storeState)){
                    area.setType("1");
                }else{
                    area.setType("2");
                }
                areaList.add(area);

                userStore.setStoreProvince(pCode);
                userStore.setStoreProvinceName(pName);
                userStore.setStoreCity(cCode);
                userStore.setStoreCityName(cName);
                userStore.setStoreDistrict(aCode);
                userStore.setStoreDistrictName(aName);
            }
            if(areaList.size() > 0){
                zsyhUserRepairareaService.deleteZsyhUserRepairareaByRepairId(userStore.getId());//先删除原有的，然后再保存新的
                zsyhUserRepairareaService.insertZsyhUserRepairareaBatch(areaList);
            }
        }

        if(EmptyUtils.isNotEmpty(userStore.getCategoryJson())){//服务类目不为空
            //解析服务类目json数据
            List<Map<String, Object>> list = JSON.parseObject(userStore.getCategoryJson(), new TypeReference<List<Map<String, Object>>>() {});
            List<ZsyhStoreCategory> catList = new ArrayList<>();
            for(Map<String, Object> a:list){
                String categoryName = String.valueOf(a.get("categoryName"));
                String categoryPrice = String.valueOf(a.get("categoryPrice"));

                ZsyhStoreCategory category = ZsyhStoreCategory.getInstance();
                category.setId(IdWorker.getId());
                category.setStoreId(userStore.getId());
                category.setCategoryName(categoryName);
                category.setCategoryPrice(categoryPrice);
                category.setCreateTime(date);
                catList.add(category);
            }
            if(catList.size() > 0){
                zsyhStoreCategoryService.deleteZsyhStoreCategoryByStoreId(userStore.getId());//先删除原有的，然后再保存新的
                zsyhStoreCategoryService.insertZsyhStoreCategoryBatch(catList);

            }
        }

        //保存大区会长id
        if(EmptyUtils.isNotEmpty(userStore.getStoreDistrict())){
            ZsyhAgentArea zsyhAgentArea = new ZsyhAgentArea();
            zsyhAgentArea.setAgentDistrict(areaId);
            List<ZsyhAgentArea> agentAreaList = zsyhAgentAreaService.selectZsyhAgentAreaList(zsyhAgentArea);
            if(agentAreaList.size() > 0){
                userStore.setAgentId(agentAreaList.get(0).getAgentId());
                //市县 同步到对应的会长名称
                ZsyhUserRepairstore store1 = new ZsyhUserRepairstore();
                store1.setId(id);
                store1.setAgentId(agentAreaList.get(0).getAgentId());
                zsyhUserRepairstoreService.updateZsyhUserRepairstoreApplet(store1);

            }




        }
        userStore.setStoreClassify("1");
        zsyhUserRepairstoreService.updateZsyhUserRepairstoreApplet(userStore);
        return success("成功");
    }

    /**
     * 获取店铺浏览记录 个人中心
     * @return
     */
    @RequestMapping("/getBrowseRecords")
    @ResponseBody
    public AjaxResult getBrowseRecords( String ids, String pageNum, String pageSize)throws Exception
    {
        ZsyhUserRepairstore userPartsstore = new ZsyhUserRepairstore();
//        userPartsstore.setLat(lat);
//        userPartsstore.setLon(lon);
        userPartsstore.setStoreState("2");

        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        if(EmptyUtils.isNotEmpty(ids)){
            map.put("ids", Convert.toStrArray(ids));
        }
        userPartsstore.setParams(map);

        return success("成功", zsyhUserRepairstoreService.selectZsyhUserRepairstoreListApplet(userPartsstore));
    }


    /**
     * 获取附近鉴定点
     * @return
     */
    @RequestMapping("/getNearbyStoreAppraisal")
    @ResponseBody
    public AjaxResult getNearbyStoreAppraisal(String userId, String pageNum, String pageSize)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);




        ZsyhUserRepairstore userPartsstore = new ZsyhUserRepairstore();
//        userPartsstore.setLat(lat);
//        userPartsstore.setLon(lon);
        userPartsstore.setStoreState("2");//审核通过的店铺
        userPartsstore.setIsAppraisal("1");//有配件鉴定权的店铺
        if(store != null){
            String code = store.getStoreDistrict();
            userPartsstore.setStoreDistrict(code);
        }

        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
//        map.put("juli", "小于100");
        map.put("jianding", "是");
        userPartsstore.setParams(map);
        return success("成功", zsyhUserRepairstoreService.selectZsyhUserRepairstoreListApplet(userPartsstore));
    }


    /**
     * 获取店铺的服务管理信息
     * @return
     */
    @RequestMapping("/getStoreCateGoryByUserId")
    @ResponseBody
    @Transactional
    public AjaxResult getStoreCateGoryByUserId(String userId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();
        ZsyhStoreCategory category = new ZsyhStoreCategory();
        category.setStoreId(repairId);
        List<ZsyhStoreCategory> list = zsyhStoreCategoryService.selectZsyhStoreCategoryList(category);
        return AjaxResult.success("获取成功", list);
    }


    /**
     * 修改服务管理信息
     * @return
     */
    @RequestMapping("/updateStoreCateGory")
    @ResponseBody
    @Transactional
    public AjaxResult updateStoreCateGory(String userId, String categoryJson)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();
        Date date = new Date();
        //解析服务类目json数据
        List<Map<String, Object>> list = JSON.parseObject(categoryJson, new TypeReference<List<Map<String, Object>>>() {});
        List<ZsyhStoreCategory> catList = new ArrayList<>();
        for(Map<String, Object> a:list){
            String categoryName = String.valueOf(a.get("categoryName"));
            String categoryPrice = String.valueOf(a.get("categoryPrice"));

            ZsyhStoreCategory category = ZsyhStoreCategory.getInstance();
            category.setId(IdWorker.getId());
            category.setStoreId(repairId);
            category.setCategoryName(categoryName);
            category.setCategoryPrice(categoryPrice);
            category.setCreateTime(date);
            catList.add(category);
        }
        if(catList.size() > 0){
            zsyhStoreCategoryService.deleteZsyhStoreCategoryByStoreId(repairId);//先删除原有的，然后再保存新的
            zsyhStoreCategoryService.insertZsyhStoreCategoryBatch(catList);

        }
        return AjaxResult.success("成功");
    }


    /**
     * 获取服务分类 前两级别树形结构
     * @return
     * @throws Exception
     */
    @RequestMapping("/getServiceTypeTreeSecond")
    @ResponseBody
    public AjaxResult getServiceTypeTreeSecond()throws Exception
    {
        List<ZsyhServiceType> list = zsyhServiceTypeService.selectZsyhServiceTypeList(new ZsyhServiceType());
        List<ZsyhServiceType> aList = new ArrayList<>();
        for (ZsyhServiceType a : list)
        {
            if(!"3".equals(a.getServiceLevel())){
                if("0".equals(a.getParentId())){
                    ZsyhServiceType type = new ZsyhServiceType();
                    type.setId(a.getId());
                    type.setServiceName(a.getServiceName());
                    List<ZsyhServiceType> bList = new ArrayList<>();
                    for (ZsyhServiceType b : list){
                        if(a.getId().equals(b.getParentId())){
                            bList.add(b);
                        }
                    }
                    type.setTypeList(bList);
                    aList.add(type);
                }
            }
        }
        return AjaxResult.success("获取成功", aList);
    }


    /**
     * 获取市县管理商的管辖区域-- 省
     * @return
     */
    @RequestMapping("/getRakerProvinceByUserId")
    @ResponseBody
    @Transactional
    public AjaxResult getRakerProvinceByUserId(String userId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        if(store == null){
            return error("您不是市县管理商");
        }
        String rakerId = store.getId();//市县id
        List<Map> areaList = zsyhUserRepairareaService.getRakerAreaListProvince(rakerId);
        return success("成功", areaList);
    }

    /**
     * 获取市县管理商的管辖区域-- 市
     * @return
     */
    @RequestMapping("/getRakerCityByUserId")
    @ResponseBody
    @Transactional
    public AjaxResult getRakerCityByUserId(String userId, String pId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        if(store == null){
            return error("您不是市县管理商");
        }
        String rakerId = store.getId();//市县id

        List<Map> areaList = zsyhUserRepairareaService.getRakerAreaListCity(rakerId, pId);
        return success("成功", areaList);
    }

    /**
     * 获取市县管理商的管辖区域-- 区县
     * @return
     */
    @RequestMapping("/getRakerAreaByUserId")
    @ResponseBody
    @Transactional
    public AjaxResult getRakerAreaByUserId(String userId, String pId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        if(store == null){
            return error("您不是市县管理商");
        }
        String rakerId = store.getId();//市县id

        List<Map> areaList = zsyhUserRepairareaService.getRakerAreaListArea(rakerId, pId);
        return success("成功", areaList);
    }

    /**
     * 获取市县管理商的管辖区域列表
     * @return
     */
    @RequestMapping("/getRakerAreaListByUserId")
    @ResponseBody
    @Transactional
    public AjaxResult getRakerAreaListByUserId(String userId, String pId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        if(store != null){
            String rakerId = store.getId();//市县id
            ZsyhUserRepairarea area = new ZsyhUserRepairarea();
            area.setRepairId(rakerId);
            area.setType("2");
            List<ZsyhUserRepairarea> areaList = zsyhUserRepairareaService.selectZsyhUserRepairareaList(area);
            return success("成功", areaList);
        }
        return error("无管辖区域");

    }

    /**
     * 获取接单区域
     * @return
     */
    @RequestMapping("/getRepairAreaListByUserId")
    @ResponseBody
    @Transactional
    public AjaxResult getRepairAreaListByUserId(String userId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        if(store != null){
            return success("成功", store.getZsyhUserRepairareaList());
        }
        return error("没有接单区域");
    }


    public static void main(String args[])
    {
        List<String> cs = new ArrayList<>();
        cs.add("维修");cs.add("清洗");cs.add("拆机");cs.add("移机");cs.add("安装");
        cs.add("空调");cs.add("洗衣机");cs.add("油烟机");cs.add("冰箱");cs.add("冰柜");
        String a = "空调维修，维修空调，维修洗衣机，安装空调，冰箱维修";
        System.out.print("原句：");
        System.out.print(a + '\n');
        System.out.print("拆分后为：");
        String[] cs2 = new String[100]; //结果数组
        int jud=0;//找到匹配字符串与否的标志
        int j=0;
        String temp=null;//初始化临时字符串
        for(;a.length()>0;)
        {
            for(int i = 0;i<a.length();i++)
            {
                temp = a.substring(i);//每次截取掉首个字符
                if(isin(cs,temp) == true)//如果目标字符串在数组中
                {
                    cs2[j] = temp;
                    jud = 1;
                    int number = temp.length();
                    a = a.substring(0,a.length()-number);
                }
            }
            if(jud == 0)//没有找到匹配字符串
            {
                cs2[j] = a.substring(a.length()-1,a.length());//将最后一个元素放在cs2里面
                a = a.substring(0, a.length()-1);//截掉最后一个元素继续循环。
            }
            jud = 0;
            j++;
        }
        for(;j >= 0;j--)
        {
            if(cs2[j] != null)
                System.out.print(cs2[j]+"   ");
        }
    }

    /*

     * 下面为判断字符串是否在词典中的函数方法

     */

    public static boolean isin(List<String> cs,String temp)//判断目标字符串是否在对比字符串数组中
    {
        int i;
        for(i = 0;i<cs.size();)
        {
            if(temp.equals(cs.get(i)))
                i = cs.size()+1;
            else
                i++;
        }
        if(i == cs.size()+1)
            return true;
        else
            return false;
    }

    /**
     * 市县获取自己管辖区域下的 师傅
     * @return
     */
    @RequestMapping("/getRepairStoreByRakerId")
    @ResponseBody
    public AjaxResult getRepairStoreByRakerId(String userId, String orderId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String rakerId = store.getId();//市县id

        //查询市县管理商管辖的所有的师傅
        List<ZsyhUserRepairstore> storeList = zsyhUserRepairstoreService.getRepairStoreByRakerId(rakerId);

        return success("成功", storeList);
    }

    /**
     * 判断师傅能否申请网点，鉴定点
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getShopNotCompleteNum")
    @ResponseBody
    @Transactional
    public AjaxResult getShopNotCompleteNum(String userId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();//师傅的店铺id

        String storeState = store.getStoreState();//网点 状态（0未申请 1待审核 2通过 3驳回）
        String appraisalState = store.getAppraisalState();//鉴定点 状态（0未申请 1待审核 2通过 3驳回）
        if("1".equals(storeState)){
            return error("网点申请正在审核中，请耐心等待");
        }
        if("1".equals(appraisalState)){
            return error("鉴定点申请正在审核中，请耐心等待");
        }

        //未完成的售后单
        ZsyhServiceOrder order = new ZsyhServiceOrder();
        order.setRepairStoreId(repairId);
        Map params = new HashMap();
        params.put("orderState", "未完成");
        order.setParams(params);
        List<ZsyhServiceOrder> orderList = zsyhServiceOrderService.selectZsyhServiceOrderList(order);//未完成的售后单
        if(orderList.size() > 0){
            return error("暂有未处理完成的厂家售后单，请处理完成后在申请。");
        }

        //未完成的个人单子
        ZsyhPersonOrder personOrder = new ZsyhPersonOrder();
        personOrder.setRepairStoreId(repairId);
        Map map = new HashMap();
        map.put("orderState", "未完成");
        personOrder.setParams(map);
        List<ZsyhPersonOrder> personOrderList = zsyhPersonOrderService.selectZsyhPersonOrderList(personOrder);
        if(personOrderList.size() > 0){
            return error("暂有未处理完成的个人订单，请处理完成后在申请。");
        }

        List<ZsyhPartsQrcodeInstall> installList = zsyhPartsQrcodeInstallService.getInstallNotApplyByRepairId(repairId);
        if(installList.size() > 0){
            return error("暂有未完成的补助金申请，请处理完成后再申请。");
        }


        return success("申请后将不能在接单，也不能申请补助金，确定申请吗？");
    }



    /**
     * 保存用户的邀请码
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/savePromotionCode")
    @ResponseBody
    @Transactional
    public AjaxResult savePromotionCode(String userId, String promotionCode)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();//师傅的店铺id
        ZsyhPlatformUser user = zsyhPlatformUserService.selectZsyhPlatformUserByPromotionCode(promotionCode);
        if(EmptyUtils.isEmpty(user)){
            return error("邀请码不存在");
        }
        if(store != null){
            store = new ZsyhUserRepairstore();
            store.setId(repairId);
            store.setSysUserid(user.getId());
            store.setSysUserName(user.getUserNickname());
            zsyhUserRepairstoreService.updateZsyhUserRepairstoreApplet(store);
        }
        return success("成功");
    }

}
